package com.javaspeak.designpatterns.go4.creational.builder;

/**
 * This builder configures the Shape class in the parent ShapeBuilder class
 * to model a Triangle.
 *
 * @author John Dickerson
 */
public class TriangleBuilder extends ShapeBuilder {

    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.creational.builder.
     *      ShapeBuilder#setName()
     */
    @Override
    public void setName() {

        this.shape.name = "Triangle";
    }

    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.creational.builder.
     *      ShapeBuilder#buildPoints()
     */
    @Override
    public void buildPoints() {

        //         1
        //       1   1
        //     1       1
        //   1 1 1 1 1 1 1

        this.shape.points = new int[4][7];
        this.shape.points[ 0 ][ 3 ] = 1;
        this.shape.points[ 1 ][ 2 ] = 1;
        this.shape.points[ 1 ][ 4 ] = 1;
        this.shape.points[ 2 ][ 1 ] = 1;
        this.shape.points[ 2 ][ 5 ] = 1;
        this.shape.points[ 3 ][ 0 ] = 1;
        this.shape.points[ 3 ][ 1 ] = 1;
        this.shape.points[ 3 ][ 2 ] = 1;
        this.shape.points[ 3 ][ 3 ] = 1;
        this.shape.points[ 3 ][ 4 ] = 1;
        this.shape.points[ 3 ][ 5 ] = 1;
        this.shape.points[ 3 ][ 6 ] = 1;
    }

    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.creational.builder.
     *      ShapeBuilder#setPaintingLevel()
     */
    @Override
    public void setPaintingLevel() {

        shape.paintLevel = 2;
    }

    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.creational.builder.
     *      ShapeBuilder#translateCoordinates()
     */
    @Override
    public void translateCoordinates() {

        // translate (4,4)
        int xTranslate = 2;
        int yTranslate = 1;

        int[][] translatedPoints =
            new int[ this.shape.points.length + yTranslate ]
                       [ this.shape.points[ 0 ].length + xTranslate ];

        for ( int y=0; y<this.shape.points.length; y++ ) {

            for ( int x=0; x<this.shape.points[ y ].length; x++ ) {

                translatedPoints[ y + yTranslate ][ x + xTranslate ] =
                    this.shape.points[ y ][ x ];
            }
        }

        this.shape.points = translatedPoints;
    }
}
